package com.zach.common.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.zach.common.entity.User;
import com.zach.common.service.UserService;

@Component
@Scope(value="request")
public class UserAction extends BaseWildAction {
	private static final long serialVersionUID = 1L;
	private static Log log = LogFactory.getLog(UserAction.class.getName());
	private static boolean logDebugFlag = log.isDebugEnabled();
	private UserService userService;
	private User user;
	private Integer pageSize;
	private Integer pageNo;
//	private List<User> userList;
	public UserAction(){
//		user = new User();
	}
	public String addUser() throws Exception {
		if(getTypeFlag().equals("init")){
			return SUCCESS;
		}
		user = new User();
		fillObjByRequestParams(user);
		userService.addUser(user);
		
		addActionMessage("添加用户成功！");
		return SUCCESS;
	}
	public String addUserJson() throws Exception {
		Map<String, Object> resultParams = new HashMap<String, Object>();
		try{
			addUser();
			resultParams.put("code", JSON_OK);
		}catch (Exception e) {
			log.error("添加用户时出现异常", e);
			resultParams.put("code", JSON_ERROR);
			resultParams.put("codeDesc", e.getLocalizedMessage());
		}
		ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(resultParams).toString());
		return NONE;
	}
	public String updateUser() throws Exception {
		user = new User();
		fillObjByRequestParams(user);
		userService.updateUser(user);
		addActionMessage("更新用户成功");
		return SUCCESS;
	}
	public String updateUserJson() throws Exception {
		Map<String, Object> resultParams = new HashMap<String, Object>();
		try {
			updateUser();
			resultParams.put("code", JSON_OK);
		} catch (Exception e) {
			log.error("更新用户异常",e);
			resultParams.put("code", JSON_ERROR);
			resultParams.put("codeDesc", e.getLocalizedMessage());
		}
		ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(resultParams).toString());
		return NONE;
	}
	
	public String delUser() throws Exception {
		String loginName = request.getParameter("loginName");
		userService.deleteUser(loginName);
		addActionMessage("删除用户成功！");
		return SUCCESS;
	}
	
	public String delUserJson() throws Exception {
		Map<String, Object> resultParams = new HashMap<String, Object>();
		try{
			delUser();
			resultParams.put("code", JSON_OK);
		}catch (Exception e) {
			log.error("删除用户异常",e);
			resultParams.put("code", JSON_ERROR);
			resultParams.put("codeDesc", e.getLocalizedMessage());
		}
		ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(resultParams).toString());
		return NONE;
	}
	
	public String getUserList() throws Exception {
		log.info("classId:"+this.getClass());
		user = new User();
		fillObjByRequestParams(user);
		List<User> userList = userService.getUserListByUser(user,getPageSize(),getPageNo());
		setSessionValue("userList", userList);
		if(logDebugFlag){
			log.debug("userList size:"+userList.size());
		}
		return SUCCESS;
	}
	
	public String getUserListJson() throws Exception {
//		log.info("classId:"+this.getClass());
		user = new User();
		Map<String, Object> resultParams = new HashMap<String, Object>();
		fillObjByRequestParams(user);
		
		List<User> userList = null;
		try {
			userList = userService.getUserListByUser(user,getPageSize(),getPageNo());
			resultParams.put("code", JSON_OK);
			resultParams.put("datas", userList);
		} catch (Exception e) {
			log.error("获取用户列表异常",e);
			resultParams.put("code", JSON_ERROR);
			resultParams.put("codeDesc", e.getLocalizedMessage());
		}
		ServletActionContext.getResponse().getWriter().print(JSONObject.fromObject(resultParams).toString());
//		setSessionValue("userList", userList);
		if(logDebugFlag){
			log.debug("userList size:"+userList.size());
		}
		return NONE;
	}
	
	@Override
	public String execute() throws Exception {
		return EMPTY;
	}
	
	public UserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public Integer getPageSize() {
		return pageSize;
	}
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
}
